/**
 * Search class
 *
 */

/** 
 * Obtains URL 'GET' parameters by name.
 *
 * Big thanks to James Padolsey for this simple GET solution.
 *
 * @see http://james.padolsey.com/javascript/bujs-1-getparameterbyname/
 *
 */
function getParameterByName(name) {
	var match = RegExp('[?&]' + name + '=([^&]*)').exec(window.location.search);
	return match && decodeURIComponent(match[1].replace(/\+/g, ' '));
}

/** initializes the search box */
$('form[name="search"]').submit(function(e) { search(e); });

/** called when search is submitted */
function search(e) {
	var query = $('input[name="q"]').val();
	var validString = /^[\s\d\w'_]*$/i.test( query );

	if( (! validString ) || query == '') {
		e.preventDefault();
		alert("bad search string");
		return false;
	}
	
	// add to search history
	var u = User.read();
	u.addSearch( query );
	User.write( u );
	return true;
}

/** goes to the details page for a movie */
function details(movieId) {
	var u = User.read();
	u.incMovie(movieId, 3);
	User.write( u );
	window.location = configs.site_base_url + 'detail.html?id=' + movieId;
}